package com.ruikong.dazhi.controller.base;

import com.github.s7connector.api.S7Serializable;
import com.github.xingshuangs.iot.protocol.s7.serializer.S7Serializer;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.plc.PLCDB1Data;
import com.ruikong.dazhi.entity.plc.PLCDB2Data;
import com.ruikong.dazhi.entity.plc.PLCDB3Data;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: jiuyanng
 * @BelongsPackage: com.ruikong.dazhi.controller.base
 * @Author: Hassion.Tang
 * @CreateTime: 2024-06-20  22:31
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/data")
@EnableAutoConfiguration
@Api(tags = "plc控制")
public class PLCController {

    @Autowired
    private S7PLC s7PLC;


    /**
     * @description:获取数据反馈值-DB2
     * @author: Hassion.Tang
     * @date: 2024/6/20 22:33
     * @param: []
     * @return: []
     **/
    @GetMapping("/getDB2Data")
    @ApiOperation("获取数据反馈值")
    public BaseResult getDB2Data() {
        S7Serializer s7Serializer = S7Serializer.newInstance(s7PLC);
        PLCDB2Data plcdb2Data = s7Serializer.read(PLCDB2Data.class);
        return BaseResult.OK(plcdb2Data);
    }


    /**
     * @description:向DB1写入值(参数设置)
     * @author: Hassion.Tang
     * @date: 2024/6/20 23:06
     * @param: [plcdb1Data]
     * @return: [com.ruikong.dazhi.entity.plc.PLCDB1Data]
     **/
    @PostMapping("/writeDB1Data")
    @ApiOperation("向DB1写入值(参数设置)")
    public BaseResult writeDB1Data(@RequestBody PLCDB1Data plcdb1Data) {
        S7Serializer s7Serializer = S7Serializer.newInstance(s7PLC);
        s7Serializer.write(plcdb1Data);
        return BaseResult.ok();
    }

    /**
     * @description:向DB3写入值（控制命令）
     * @author: Hassion.Tang
     * @date: 2024/6/20 23:15
     * @param: []
     * @return: []
     **/
    @PostMapping("/writeDB3Data")
    @ApiOperation("向DB3写入值（控制命令）")
    public BaseResult writeDB3Data(@RequestBody PLCDB3Data plcdb3Data) {
        S7Serializer s7Serializer = S7Serializer.newInstance(s7PLC);
        s7Serializer.write(plcdb3Data);
        return BaseResult.ok();
    }

    /**
     * @description: 根据DB块和地址获取数据
     * @author: Hassion.Tang
     * @date: 2024/6/20 23:21
     * @param:
     * @return:
     **/
    @PostMapping("/readByAddress")
    @ApiOperation("根据地址读取数据")
    public BaseResult readByAddress(@RequestParam("address") String address, @RequestParam("dataType") String dataType) {
        Object data = null;
        switch (dataType) {
            case "bool":
                data = s7PLC.readBoolean(address);
                break;
            case "real":
                data = s7PLC.readFloat32(address);
                break;
            case "int":
                data = s7PLC.readInt16(address);
                break;
        }
        return BaseResult.OK(data);
    }



    /**
     * @description:根据地址写入数据
     * @author: Hassion.Tang
     * @date: 2024/6/20 23:53
     * @param: [address, dataType, data]
     * @return: [java.lang.String, java.lang.String, java.lang.Object]
     **/
    @PostMapping("/writeByAddress")
    @ApiOperation("根据地址写入数据")
    public BaseResult writeByAddress(@RequestParam("address")String address, @RequestParam("dataType") String dataType,@RequestParam("data")Object data){
        switch (dataType) {
            case "bool":
               s7PLC.writeBoolean(address,Boolean.parseBoolean(data.toString()));
                break;
            case "real":
                s7PLC.writeFloat32(address,Float.parseFloat(data.toString()));
                break;
            case "int":
                s7PLC.writeInt32(address,Integer.parseInt(data.toString()));
                break;
        }
        return BaseResult.ok();
    }


}
